package com.ggh.jinjilive.ui.mall.goods.details;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.widget.NestedScrollView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityGoodsDetailsBinding;
import com.ggh.jinjilive.dialog.SpecificationsDialog;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.goods.GoodsDetailsMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.ggh.jinjilive.ui.mall.goods.affirm.GoodsAffirmOrderActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.views.banner.ImageAdapter;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.youth.banner.indicator.CircleIndicator;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品详情
 */
public class GoodsDetailsActivity extends BaseActivity<ActivityGoodsDetailsBinding> implements View.OnClickListener{
    //商品VM
    private GoodsDetailsViewModel goodsDetailsViewModel;
    //传输的实体
    private MallGoods mallGoods;
    //社区ID
    private int communityID = 0;
    //规格
    private SpecificationsDialog specificationsDialog;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        goodsDetailsViewModel = new ViewModelProvider(this,viewModelFactory).get(GoodsDetailsViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_goods_details;
    }


    @Override
    public void initParam() {
        super.initParam();
        mallGoods = (MallGoods) getIntent().getSerializableExtra("mallGoods");
        communityID = getIntent().getIntExtra("communityID",0);
    }

    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llDistance.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

        loadService = LoadSir.getDefault().register(binding.llProductInfo, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //商品详情
                goodsDetailsViewModel.productInfo(mallGoods.getId());
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //商品详情
        goodsDetailsViewModel.productInfo(mallGoods.getId());
        //获取屏幕宽
        ViewGroup.LayoutParams params =  binding.banner.getLayoutParams();
        params.height = ScreenUtils.getScreenWidth();
        binding.banner.setLayoutParams(params);


    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //关闭
        binding.imgOcclude.setOnClickListener(this);
        //规格
        binding.llSpecifications.setOnClickListener(this);
        //店铺
        binding.tvStore.setOnClickListener(this);
        //收藏
        binding.tvCollection.setOnClickListener(this);
        //购物车
        binding.tvShopping.setOnClickListener(this);
        //加入购物车
        binding.ButtonJoinShopping.setOnClickListener(this);
        //立即下单
        binding.ButtonPlace.setOnClickListener(this);
        //商品详情
        goodsDetailsViewModel.mGoodsDetailsMobile.observe(this, new Observer<GoodsDetailsMobile>() {
            @Override
            public void onChanged(GoodsDetailsMobile goodsDetailsMobile) {
                if(null != goodsDetailsMobile){
                    //规格弹框
                    specificationsDialog = new SpecificationsDialog(mContext,goodsDetailsMobile);
                    loadService.showSuccess();
                    //添加生命周期观察者
                    binding.banner.addBannerLifecycleObserver(GoodsDetailsActivity.this);
                    //展示
                    binding.banner.setAdapter(new ImageAdapter(goodsDetailsMobile.getString()));
                    ////设置指示器
                    binding.banner.setIndicator(new CircleIndicator(mContext));
                    //价格
                    binding.tvPrice.setText(goodsDetailsMobile.getPrice() +"");
                    //购买人数
                    binding.tvSold.setText(goodsDetailsMobile.getSales() +"人已购买");
                    //看过
                    binding.tvSeen.setText(goodsDetailsMobile.getSee() + "人已看过");
                    //产品介绍
                    binding.name.setText(goodsDetailsMobile.getDescribes());
                    //判断是否收藏
                    if(goodsDetailsMobile.getIs_collect() == 0){
                        goodsDetailsViewModel.mCollection.setValue(false);
                    }else {
                        goodsDetailsViewModel.mCollection.setValue(true);
                    }
                    //店铺图片
                    Glide.with(binding.riv).load(goodsDetailsMobile.getStore_img()).into(binding.riv);
                    //店铺名称
                    binding.storeName.setText(goodsDetailsMobile.getStore_name());
                }else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        });
        //取消收藏成功
        goodsDetailsViewModel.mCollection.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    binding.tvCollection.setText("已收藏");
                    binding.tvCollection.setTextColor(Color.parseColor("#DF2E22"));
                    Drawable drawable = getResources().getDrawable(R.mipmap.icon_collection);
                    binding.tvCollection.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);
                }else {
                    binding.tvCollection.setText("收藏");
                    binding.tvCollection.setTextColor(Color.parseColor("#FFFFFF"));
                    Drawable drawable = getResources().getDrawable(R.mipmap.icon_not_collection);
                    binding.tvCollection.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);
                }
            }
        });

        //滑动监听
        binding.nsllRolling.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_occlude://关闭
                finish();
                break;
            case R.id.tv_store://店铺
                if(null != mallGoods){
                    ARouter.getInstance().build(ARouterUtils.ShopActivity)
                            .withInt("ShopId",mallGoods.getSid())
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }
                break;

            case R.id.ll_specifications://规格
            case R.id.Button_join_shopping://加入购物车
            case R.id.Button_Place://立即下单
                bouncedSpecifications();
                break;
            case R.id.tv_collection://收藏
                if(goodsDetailsViewModel.mCollection.getValue()){
                    //取消收藏成功
                    goodsDetailsViewModel.delCollectProduct(mallGoods.getId());
                }else {
                    //收藏成功
                    goodsDetailsViewModel.addCollectProduct(mallGoods.getId());
                }
                break;
        }
    }

    /**
     * 规格
     */
    public void bouncedSpecifications(){
        if(null != specificationsDialog && !specificationsDialog.isShowing()){
            specificationsDialog.show();
            specificationsDialog.setAddItemOnClickListener(new AddItemOnClickListener() {
                @Override
                public void onSkipClicked(int position, Object mObject) {
                    if(position == 0){//选择了规则
                        GoodsSpecificationMobile goodsSpecificationMobile = (GoodsSpecificationMobile) mObject;
                        if(null != goodsSpecificationMobile){
                            goodsDetailsViewModel.mGoodsSpecificationMobile.set(goodsSpecificationMobile);
                            //规格
                            binding.tvSpecifications.setText(goodsSpecificationMobile.getName());
                        }
                    }else if(position == 1){//取消规格
                        goodsDetailsViewModel.mGoodsSpecificationMobile.set(null);
                        //规格
                        binding.tvSpecifications.setText("选择规格");
                    }else if(position == 2){//加入购物车
                        GoodsSpecificationMobile goodsSpecificationMobile = (GoodsSpecificationMobile) mObject;
                        Map<String, Object> map = new HashMap<>();
                        //商品ID
                        map.put("product_id",mallGoods.getId());
                        //购买数量
                        map.put("num",goodsSpecificationMobile.getGoodsQuantity());
                        //规格ID
                        map.put("product_spec_id", goodsSpecificationMobile.getId());
                        if(communityID > 0){
                            //社区ID
                            map.put("cid", communityID);
                        }
                        goodsDetailsViewModel.addCart(map);

                    }else if(position == 3){//立即下单
                        GoodsSpecificationMobile goodsSpecificationMobile = (GoodsSpecificationMobile) mObject;
                        if(null != goodsSpecificationMobile){
                            ARouter.getInstance().build(ARouterUtils.GoodsAffirmOrderActivity)
                                    .withSerializable("goodsDetailsMobile",goodsDetailsViewModel.mGoodsDetailsMobile.getValue())
                                    .withSerializable("GoodsSpecificationMobile",goodsSpecificationMobile)
                                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                    .navigation();
                        }
                    }
                }
            });
        }
    }
}
